<?php

/**
 * component for widget functionality
 */
class widgetsComponents extends sfComponents {
    public function executeWidget(sfWebRequest $request){
        $widgetSlug=$this->widget->getSlug();
        if (method_exists($this, $widgetSlug)){
            foreach ($this->widgetPosition->toArray() as $key => $var){
                $this->$key=$var;
            }

            return $this->$widgetSlug($request);
        }
    }


    private function test(sfWebRequest $request){

    }

    /*
     * from which section
     * count limit
     * parts to show
     *
     */
    private function aggregator(sfWebRequest $request){
        // @todo remove $this->entries_section_id, use array $this->entries_section_ids
        if (!isset($this->entries_section_ids) && (!isset($this->entries_section_id) || $this->entries_section_id<1)){
            return sfView::NONE;
        }
	    
        $sectionsQ=Doctrine_Core::getTable('Section')
                        ->createQueryForBackend();
        if (isset($this->entries_section_ids) && is_array($this->entries_section_ids) && sizeof($this->entries_section_ids)>0){
            $sectionsQ->andWhereIn('id', $this->entries_section_ids);
        }else{
            $sectionsQ->andWhere('id = ?', $this->entries_section_id);
        }

        $this->sections=$sectionsQ->execute();
	    $this->section = $this->sections[0];


        $q=Doctrine_Core::getTable('Entry')->createQuery();
        $q->addWhereSectionId($this->entries_section_id);

        if (isset($this->entries_count) && $this->entries_count>0){
            $q->limit($this->entries_count);
        }

        $q->orderBy("id DESC");

        $this->items=$q->execute();
    }

	private function entries_menu(sfWebRequest $request)
	{
//		$entries=$this->entries;

//		if(!empty($this->currentSection) && count($entries) > 0)
//		{
//
//			foreach($entries as $entry)
//			{
//
//
//
//			}
//		}
	}


    private function menu(sfWebRequest $request){
        $this->sections=Doctrine_Core::getTable('Section')->createQueryForBackend()->execute();

        if (!empty($this->currentSection)){
            $entries=Doctrine::getTable("Entry")->createQuery()->addWhereSection($this->currentSection)->execute();
        }

        /** @todo refactor */
		// dirty solution to get all tags in Sections Entries
		// later will be rewritten to full select with joins
        //  select distinct tag.* from entry, tag, tagging where entry.section_id=1 and tag.id=tagging.tag_id and entry.id=tagging.taggable_id and tagging.taggable_model="Entry"
        $this->tags=null;
        if (!empty($this->currentSection) && !empty($entries) && sizeof($entries)>1){ // no point to show tags, when there is only one Entry
//        if (!empty($this->currentSection)){ // no point to show tags, when there is only one Entry
            $this->tags=array();
            foreach ($entries as $entry){
                /* @var $entry Entry */
                $tags=$entry->getSavedTagsQuery()->execute();
                foreach ($tags as $tag){
                    if (!in_array($tag, $this->tags)){
                        $tag->setSection($this->currentSection);// filling tags with currentSection for Twig to get url from it later
                        $this->tags[]=$tag;
                    }
                }
            }
        }


    }


    private function social(sfWebRequest $request){
        /*
        items from settings:
            widgetPosition->getTwitterUrl()
            widgetPosition->getTwitterImage()
            widgetPosition->getFacebookUrl()

        input exampel:
            array(
                "twitter_url" => "http://twitter.com/nasilis",
                "facebook_url" => "http://www.facebook.com/profile.php?id=100000160629299",
                "facebook_icon" => "/images/icons/custom_facebook.png"
            )

         outputs to Twig:
            array(
                "twitter" => array("url" => "http://twitter.com/nasilis"),
                "facebook" => array(
                    "url" => "http://www.facebook.com/profile.php?id=100000160629299",
                    "icon" => "/images/icons/custom_facebook.png" 
                ),
            )
         */

        $defaultIcons=array(
            "twitter"   =>  "/images/icons/twitter.png",
            "facebook"  =>  "/images/icons/facebook.png",
        );

        $items=array();
        $widgetSettings=$this->widgetPosition->toArray();
        foreach ($widgetSettings as $key => $var){
            $socialUrlType=self::socialGetItemType($key);
            if ($socialUrlType!==false){
                $settingType=str_replace("{$socialUrlType}_", "", $key);// removes social type from key: "twitter_url" becomes "url"

                if ($settingType!=""){
                    $items[$socialUrlType][$settingType]=$var;
                }
            }
        }

        if (empty($items)){
            return sfView::NONE;
        }

        // fill with default icons
        foreach ($items as $key => $item){
            if (!array_key_exists("icon", $item) || $item["icon"]==""){
                $items[$key]["icon"]=$defaultIcons[$key];
            }
        }

        $this->items=$items;
    }

    /**
     *
     *  method for use by social widget -
     *  @todo: maybe it is a good idea to place each widget in separate symfony component _file_
     *
    **/
    private static function socialGetItemType($settingsKey){
        if (strpos($settingsKey, "twitter_")!==false){
            return "twitter";
        }elseif (strpos($settingsKey, "facebook_")!==false){
            return "facebook";
        }elseif (strpos($settingsKey, "tumblr_")!==false){
            return "tumblr";
        }

        return false;
    }

}
